﻿using Customer.Bussiness_Service;
using Entity;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.Filters; 
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace OtherApi.Filter
{
    public class Api_Filter : Attribute, IActionFilter 
    {
        private readonly IToole_DI _tool;
        /// <summary>
        /// 拦截器注入点
        /// </summary>
        /// <param name="tokenService"></param>
        public Api_Filter(IToole_DI Tool)
        {
            _tool = Tool;
        }
        /// <summary>
        /// action执行之后的拦截控制器
        /// </summary>
        /// <param name="context"></param>
        public void OnActionExecuted(ActionExecutedContext context)
        {
            //Console.WriteLine("action执行之后");
        }
        /// <summary>
        /// action执行之前的拦截控制器
        /// </summary>
        /// <param name="context"></param>
        public void OnActionExecuting(ActionExecutingContext context)
        {
            if (context.HttpContext.Request.Method.ToLower() == "options")
            {
                context.Result = new ContentResult() { StatusCode = 204, Content = "..." };
            }
            //if (context.HttpContext.Request.Headers["userid"].FirstOrDefault() == null || context.HttpContext.Request.Headers["secret"].FirstOrDefault() == null)
            //{
            //    context.Result = new ContentResult() { StatusCode = 999, Content = Newtonsoft.Json.JsonConvert.SerializeObject(new { code = 999, msg = "身份认证失败, 请确认userid和secret" }) };
            //}
            //string userid = context.HttpContext.Request.Headers["userid"].FirstOrDefault();
            //string secret = context.HttpContext.Request.Headers["secret"].FirstOrDefault();
            //string sql = $"select top 1 Id,BaseUserId,Secret,UserName from dbo.[user] where  id=@id and secret=@secret";
            //var ApiUser = _tool.di_db.Get_rk_List<ApiUSER>(sql, new { id = userid, secret = secret });
            //if (ApiUser == null)
            //{
            //    context.Result = new ContentResult() { StatusCode = 999, Content = Newtonsoft.Json.JsonConvert.SerializeObject(new { code = 999, msg = "身份认证失败, 请确认userid和secret" }) };
            //}
            //验证一下是否安卓方面发过来的请求
            //var result = _token_service.AuthToken(token);
            //if (!Convert.ToBoolean(result.data))
            //{
            //    context.Result = new ContentResult() { ContentType = "application/json", StatusCode = 777, Content = Newtonsoft.Json.JsonConvert.SerializeObject(new { code = "777", msg = "token不存在或已过期 " }) };
            //}
        }
    }
}
 